package com.tim.app.manager;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;

import com.netease.nim.uikit.common.util.log.LogUtil;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.server.result.RedbagListBean;
import com.tim.app.ui.dialog.RedBagDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wdy on 2017/8/19.
 */

public class WindowUtils {
    private final String LOG_TAG = "WindowUtils";
    private View mView = null;
    private WindowManager mWindowManager = null;
    private Context mContext = null;
    public Boolean isShown = false;
    private boolean isShow;
    private static WindowUtils inStance;

    private WindowUtils() {

    }

    public synchronized static WindowUtils getInStance() {
        if (inStance == null) {
            inStance = new WindowUtils();
        }
        return inStance;
    }

    private RedBagDialog redBagDialog;
    private static int position = 0;

    /**
     * 显示弹出框
     */
    public void showPopupWindow() {
        if (list == null || list.size() == 0) {
            return;
        }
        redBagDialog = new RedBagDialog();
        Bundle bundle = new Bundle();
        bundle.putString("money", list.get(position).getMoney() + "");
        bundle.putString("id", list.get(position).getId() + "");
        bundle.putString("type", list.get(position).getType());
        bundle.putInt("position", position);
        redBagDialog.setArguments(bundle);
        redBagDialog.setOnDimissListener(onDimissListener);
        if (isShown) {
            mView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isShow) {
                        redBagDialog.dismiss();
                        isShow = false;
                    } else {
                        isShow = true;
                        redBagDialog.show(((AppCompatActivity) mContext).getSupportFragmentManager(), "");
                    }
                }
            });

            return;
        }
        isShown = true;

        mWindowManager = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        ImageView imageView = new ImageView(mContext);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, -2);
        imageView.setPadding(10, 10, 10, 10);
        imageView.setLayoutParams(layoutParams);
        imageView.setBackgroundResource(R.drawable.ic_redbag);
        mView = imageView;
        mView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShow) {
                    redBagDialog.dismiss();
                    isShow = false;
                } else {
                    isShow = true;
                    redBagDialog.show(((AppCompatActivity) mContext).getSupportFragmentManager(), "");
                }
            }
        });
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type = LayoutParams.TYPE_TOAST;
        int flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.flags = flags;
        params.softInputMode = LayoutParams.SOFT_INPUT_ADJUST_PAN;
        // 不设置这个弹出框的透明遮罩显示为黑色
        params.format = PixelFormat.TRANSLUCENT;

        params.width = LayoutParams.WRAP_CONTENT;
        params.height = LayoutParams.WRAP_CONTENT;
        // TODO: 2017/8/19  
        params.y = (int) ((RT.getScreenHeight() - 85 * RT.getDensity() - mView.getMeasuredHeight() - 700) / RT.getDensity());
        params.gravity = Gravity.RIGHT;
        mWindowManager.addView(mView, params);
    }

    public void setContext(Context context) {
        this.mContext = context;
    }

    private RedBagDialog.OnDimissListener onDimissListener = new RedBagDialog.OnDimissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            isShow = false;
            if (position != list.size()) {
                showPopupWindow();
            } else {
                //  list = null;
            }
        }

        @Override
        public void onShow() {
            hidePopupWindow();
        }

        @Override
        public void onOpenFinish(int id) {
            position = id;
            position++;
            if (position >= list.size()) {
                list.clear();
                position = 0;
                hidePopupWindow();
            } else {
                //     position++;
                redBagDialog = new RedBagDialog();
                Bundle bundle = new Bundle();
                bundle.putString("money", list.get(position).getMoney() + "");
                bundle.putString("id", list.get(position).getId() + "");
                bundle.putInt("position", position);
                redBagDialog.setArguments(bundle);
                redBagDialog.setOnDimissListener(onDimissListener);
                //   redBagDialog.show(((AppCompatActivity) mContext).getSupportFragmentManager(), "");
            }
//            if(redBagDialog!=null){
//                redBagDialog.dismiss();
//            }

        }
    };

    /**
     * 隐藏弹出框
     */
    public void hidePopupWindow() {
        if (isShown && null != mView) {
            mWindowManager.removeView(mView);
            isShown = false;
        }
    }

    private List<RedbagListBean.ResBean.ArraylistBean> list = new ArrayList<>();

    public void setRedbagList(List<RedbagListBean.ResBean.ArraylistBean> list) {
        if (this.list != null && this.list.size() != 0) {
            this.list.addAll(list);
        } else {
            this.list = list;
        }
    }

    public void clearList() {
        if (list != null && list.size() != 0) {
            list.clear();
        }
    }
}
